Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

265
Vistas
Creating a "multi-row, multi-column, multi-table" Sequelize Query

I have to get data from a Postgres DB for my project. The API is supplying a list of country codes in the form of a string like "US, IN, PK". The Table Countries has the country ID for each country code. The table user has the User ID for each user. Now the table User_Country contains the mapping i.e. if a user has permission for a certain country signified by the has_approval flag which is a Boolean. The query has to return the information of all the users who have the has_approval flag true for all the given countries I have written the following code which runs three different queries for the same. However, I think we can write a single query using Outer join.

 let countryList = countryIds.split(",");  // countryIds = "US,IN,PK"
        let countryIdList = [];
        let countryId = "";

        for(let i = 0; i < countryList.length; i++){
            countryId = await Country.findOne({
                                       attributes: ['id'],
                                       where: {
                                               ctrn_cd : countryList[i]
                                                },
                                         raw: true
                                         });
            countryIdList.push(countryId.id);
        }

        let users = await Users.findAll({
            attributes: ['id'],
            raw: true
        })

        let UserIds = [];
        for(let i = 0; i < users.length; i ++){
            let countryCount = await UserCountry.count({
                where: {
                    ctrn_id : countryIdList,
                    user_id: users[i].id,
                    has_approval: true
                },
                raw: true
            });

        if(countryCount == countryIdList.length) UserIds.push(users[i].id);
        }

        let layoutapprovers = await User.findAll({
            attributes: ['id', 'guid', ['role_id','roleId'], 'name', ['email','emailId']],
            where:{
                id: UserIds
            },
            raw: true
        });

        return layoutapprovers;```
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda